﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webnew.Common;
using webnew.BLL;

namespace webnew.Systems
{
    public partial class S_SystemTypeEdit : WebPageBase.EditBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //设置列表页面名，即 返回的页面名
            _ListSrc = "S_SystemTypeList.aspx";
            #region 隐藏母板页面按钮
            this.Master.FindControl("btnSaveBack").Visible = true;
            this.Master.FindControl("btnSaveStay").Visible = true;
            this.Master.FindControl("btnBack").Visible = true;

            #endregion
            _ListQueryString = "SystemType,RecordCount,CurrentPageIndex";
            if (!IsPostBack)
            {
                //设置页面标题
                SetTitle(true, "后台管理 >> 类别管理");

            }
        }


        /// <summary>
        /// 重写基类，显示数据方法
        /// </summary>
        protected override void ShowData()
        {
            
            Model.Sys_SystemTypeInfo model = new BLL.Sys_SystemType().GetModel(_RecordId);
            txtSort.Text=model.Sort.ToString();
            txtSystemType.Text=model.SystemType;
            txtTypeName.Text=model.TypeName;
            txtTypeValue.Text=model.TypeValue;
            ViewState["currentTypeName"] = model.TypeName; //保存当前类别名,用于验证判断
        }

        /// <summary>
        /// 重写基类，传入要验证的数据
        /// </summary>
        protected override List<PageValid> _SetValidData()
        {
            List<PageValid> lst = new List<PageValid>();
            lst.Add(new PageValid("序号", txtSort.Text.Replace(" ", ""), PageValid.validType.CommonValid, false, 10));
            lst.Add(new PageValid("序号", txtSort.Text.Replace(" ", ""), PageValid.validType.IntValid, "只能为正整数"));
           // lst.Add(new PageValid("序号", "0", txtSort.Text.Replace(" ", ""), PageValid.validType.NoSameValid, "不能为0数字"));
            lst.Add(new PageValid("系统类别", txtSystemType.Text, PageValid.validType.CommonValid, false, 100));
            lst.Add(new PageValid("类别名", txtTypeName.Text, PageValid.validType.CommonValid, false, 100));
            //判断是否存在相同的职位名称
            if (_RecordId != null) //修改
            {
                //判断用户在修改状态下是否修改过职位名称,没修改过 则跳过验证
                if (ViewState["currentTypeName"].ToString() != txtTypeName.Text)
                    lst.Add(new PageValid("类别名", "1", new BLL.Sys_SystemType().ExistSystemType(txtSystemType.Text, txtTypeName.Text), PageValid.validType.NoSameValid, "已存在,请重新输入"));
            }
            else //添加
                lst.Add(new PageValid("类别名", "1", new BLL.Sys_SystemType().ExistSystemType(txtSystemType.Text, txtTypeName.Text), PageValid.validType.NoSameValid, "已存在,请重新输入"));

            return lst;
        }

        /// <summary>
        /// 重写基类保存数据方法
        /// </summary>
        protected override int SaveData()
        {
            Model.Sys_SystemTypeInfo model = new Model.Sys_SystemTypeInfo();
            
            model.Sort = MyConvert.toInt32(txtSort.Text);
            model.SystemType = txtSystemType.Text;
            model.TypeName = txtTypeName.Text;
            model.TypeValue = txtTypeValue.Text;
            

            if (_RecordId !=null)  //修改
            {

                // 修改
                model.Id = _RecordId;
                BLL.Sys_SystemType bll = new webnew.BLL.Sys_SystemType();
                return bll.Update(model);

                
            }
            else                //添加
            {
                // 添加
                //model.Id = Guid.NewGuid().ToString();
                model.isdel = 0;
                BLL.Sys_SystemType bll = new webnew.BLL.Sys_SystemType();
                return bll.Add(model);
             
            }           
        
    }

    }
}
